/*
 * 文件名：PayExperienceController.java
 * 版权：Copyright by www.ysepay.com
 * 修改人：mosx
 * 修改时间 2016年7月22日
 */

package com.yspay.sample.controller;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yspay.sample.ApipaySubmit;
import com.yspay.sample.config.DirectPayConfig;
import com.yspay.sample.request.PayExperienceRequest;

@Controller
public class PayExperienceController {

	@Value("${openapi.web.url}")
	private String web_openapi_url;

	@Value("${pay.parameter.partner.id}")
	private String partner_id;

	@Value("${pay.parameter.notify.url}")
	private String notify_url;

	@Value("${pay.parameter.return.url}")
	private String return_url;

	@Value("${pay.parameter.timeout.express}")
	private String timeout_express;

	@Value("${pay.parameter.business.code}")
	private String business_code;

	@Value("${pay.parameter.path.parter.pkcs12}")
	private String path_parter_pkcs12;

	@Value("${pay.parameter.password.partner.pkcs12}")
	private String password_partner_pkcs12;

	@Value("${static.resource.url}")
	private String static_url;

	@RequestMapping(value = "/demo/webPayExperience.do", params = "method=PayExperience")
	public ModelAndView payExperience(
			PayExperienceRequest PayExperienceRequest,
			HttpServletRequest request) {
		Map<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("method", DirectPayConfig.METHOD);
		paramsMap.put("partner_id", partner_id);
		paramsMap.put("charset", DirectPayConfig.DEFAULT_CHARSET);
		paramsMap.put("sign_type", DirectPayConfig.SIGN_ALGORITHM);
		paramsMap.put("notify_url", static_url.concat(notify_url));
		paramsMap.put("return_url", static_url.concat(return_url));
		paramsMap.put("version", DirectPayConfig.VERSION);
		paramsMap.put("subject", DirectPayConfig.SUBJECT);
		paramsMap.put("seller_name", DirectPayConfig.SELLER_NAME);
		paramsMap.put("extra_common_param", DirectPayConfig.EXTRA_COMMON_PARAM);
		paramsMap.put("seller_id", DirectPayConfig.SELLER_ID);
		paramsMap.put("timeout_express", timeout_express);
		paramsMap.put("extend_params", DirectPayConfig.EXTEND_PARAMS);
		paramsMap.put("business_code", business_code);
		// ===================系统生成，变化==================
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		Random rand = new Random();
		// 最低一元
		float amount = 1 + rand.nextFloat() * 3;
		String timestamp = format.format(date);
		String total_amount = dcmFmt.format(amount);
		// 发送请求的时间(timestamp)
		paramsMap.put("timestamp", timestamp);
		// 银盛支付合作商户网站唯一订单号(out_trade_no)
		paramsMap.put("out_trade_no", RandomStringUtils.randomNumeric(9));
		// 订单资金总额(total_amount)
		paramsMap.put("total_amount", total_amount);
		// ===================网银直连参数==================
		if (DirectPayConfig.DIRECTNETBANK.equals(PayExperienceRequest
				.getPayExperienceType())) {
			paramsMap.put("pay_mode", DirectPayConfig.PAY_MODE);
			paramsMap.put("bank_type", PayExperienceRequest.getBank_type());
			paramsMap.put("bank_account_type",
					DirectPayConfig.BANK_ACCOUNT_TYPE);// 暂时默认借记卡
			paramsMap.put("support_card_type",
					DirectPayConfig.SUPPORT_CARD_TYPE);
		}
		Map<String, String> sPara = ApipaySubmit.buildRequestPara(request,
				paramsMap, path_parter_pkcs12, password_partner_pkcs12);
		ModelMap map = new ModelMap();
		String url = "/gateway.do?method=" + DirectPayConfig.METHOD;
		url = web_openapi_url.concat(url);
		map.put("actionUrl", url);
		Map<String, String> changeMap = new HashMap<String, String>();
		// 过滤重复的method
		sPara.remove("method");
		List<String> keys = new ArrayList<String>(sPara.keySet());
		for (int i = 0; i < keys.size(); i++) {
			String name = (String) keys.get(i);
			String value = (String) sPara.get(name);
			changeMap.put(name, StringEscapeUtils.escapeHtml(value));
		}
		map.put("params", changeMap);
		return new ModelAndView("yspayapi", map);
	}
}
